<?php
namespace app\api\controller;

use app\api\model\AddressModel;
use app\api\model\FeedbackModel;
use app\api\model\UserModel;
use think\Db;
use think\Request;
use think\response\Json;

class User extends BaseController
{
    //初始化
    public function __construct(Request $request = null)
    {
        parent::__construct();
        $this->user = new UserModel();
        $this->address = new AddressModel();
        $this->feed = new FeedbackModel();
    }


    /******************************** 我的设置 ***************************************/
    /**
     * 获取个人信息
     * @return array
     */
    public function Index()
    {
        $find = $this->user->getFind();
        return $find;
    }

    /**
     * 修改个人信息
     * @return Json
     */
    public function EditUser()
    {
        if(input('post.')){
            $res = $this->user->postEdit();
            return  $res;
        }
        $find = $this->user->getMyfind();
        return $find;
    }

    /**
     * 头像上传
     * @return Json
     */
    public function EditHead()
    {
        $file = $_FILES['image'];
        $res = FileImg($file,'/uploads/head/');
        if($res){
            AjaxReturn(1,'上传成功',$res);
        }else{
            AjaxReturn(-1,'error');
        }
    }

    /**
     * 个人推荐码
     * @return array
     */
    public function Code()
    {
        $find = $this->user->getMyfind();

        return $find;
    }





    /******************************** 消息列表 ***************************************/

    /**
     * 我的消息
     * @return object
     */
    public function UserLogs()
    {
        $list = $this->user->getLogs();
        return $list;
    }

    /**
     * 已读，删除
     * @return int
     */
    public function LogsState()
    {
        $res = $this->user->LogState();
        return $res;
    }

    /**
     * 详情
     * @return object
     */
    public function LogsFind()
    {
        $find = $this->user->logFind();
        return $find;
    }
    /******************************** 收货地址 ***************************************/
    /**
     * 我的收货地址
     * @return Json
     */
    public function Address()
    {
        $list = $this->address->getList();
        return $list;
    }

    /**
     * 添加编辑
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @return Json
     */
    public function AddEdit()
    {
        $res = $this->address->postAddEdit();
        return $res;
    }

    /**
     * 地址详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function AddFind()
    {
        $find = $this->address->getFind();
        return $find;
    }

    /**
     * 地址删除
     * @throws \think\Exception
     * @return Json
     */
    public function AddDel()
    {
        $id = input('ad_id');
        $res = Db::name('address')->where('ad_id',$id)->delete();
        if($res){
            AjaxReturn(1,'删除成功');
        }else{
            AjaxReturn(-1,'删除失败');
        }
    }



    /******************************** 账户安全 ***************************************/

    /**
     * 修改密码
     * @return Json
     */
    public function Editpwd()
    {
        $res = $this->user->postPwd();
        return $res;
    }
    /**
     * 忘记密码
     * @return Json
     */
    public function Forget_pwd()
    {
        $res = $this->user->post_forget_pwd();
        return $res;
    }

    /**
     * 修改手机号
     * @return Json
     */
    public function EditPhone()
    {
        $res = $this->user->postPhone();
        return $res;
    }

    /**
     * 提交反馈
     * @return Json
     */
    public function Feedback()
    {
        $res = $this->feed->postAdd();
        return $res;
    }
    /**
     * 反馈凭证
     * @return Json
     */
    public function FeedImg()
    {
        $file = $_FILES['image'];

        $res = FileImg($file,'/uploads/feedback/');
        if($res){
            AjaxReturn(1,'上传成功',$res);
        }else{
            AjaxReturn(-1,'error');
        }
    }
    /******************************** 团队、记录明细 ***************************************/
    /**
     * 我的团队
     * @return Json
     */
    public function Team()
    {
        $res = $this->user->get_team();
        return $res;
    }
    /**
     * 我的积分
     * @return array
     */
    public function UserPoints()
    {
        $list = $this->user->getPoints();
        return $list;
    }
    /**
     * 提现记录
     * @return array
     */
    public function UserWithdrawal()
    {
        $list = $this->user->getWithdrawal();
        return $list;
    }
}